package client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author lizuhu
 * @description 聊天室登录页面
 * @date 2021/9/8 15:53
 */
public class Login extends JFrame {
    JLabel label = new JLabel("用户名");
    JTextArea text = new JTextArea();
//    JTextArea tips = new JTextArea();
    JButton loginBtn = new JButton("登录");
    //窗口宽度
    final int WIDTH = 500;
    //窗口高度
    final int HEIGHT = 300;

    public Login() {
        this.setTitle("用户登录");
        this.setSize(WIDTH, HEIGHT);    //设置窗体大小
        this.setLocationRelativeTo(null);
        this.setResizable(false);  //窗体大小设置为不可改
        this.setVisible(true);  //设置可见
        setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int w = Toolkit.getDefaultToolkit().getScreenSize().width;
        //获取本机屏幕纵向分辨率
        int h = Toolkit.getDefaultToolkit().getScreenSize().height;
        //将窗口置中
        setLocation((w - this.WIDTH) / 2, (h - this.HEIGHT) / 2);
        label.setBounds(40, 50, 50, 30);
        this.add(label);
        text.setBounds(90, 50, 250, 30);
        text.setFont(new Font("楷体", Font.BOLD, 20));
        this.add(text);
        loginBtn.setBounds(380, 45, 80, 40);
        loginBtn.setFont(new Font("宋体", Font.BOLD, 18));
        this.add(loginBtn);
//        tips.setBounds(90, 90, 250, 100);
//        tips.setFont(new Font("楷体", Font.TRUETYPE_FONT, 12));
//        tips.setEditable(false);
//        this.add(tips);
        //监听登录按钮
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //创建连接
                ChatClient client = new ChatClient();
//                Login.this.tips.append("正在登录.....");
                while (!client.isConnect_status()) {
                    client.connect();
                    System.out.println("正在登录....");
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
                String userName = Login.this.text.getText();
                setVisible(false);
                new ClientView(client.getSocket(), userName);
            }
        });
    }

}
